package cn.fastoo.web.backstage.web.argumentResolver;

import cn.fastoo.web.backstage.param.LoginUser;
import cn.fastoo.web.backstage.service.system.ShiroDbRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;


public class LoginUserArgumentsResolver implements HandlerMethodArgumentResolver  {


	@Override
	public boolean supportsParameter(MethodParameter parameter) { 
		return parameter.getParameterType().equals(LoginUser.class);
	}

	@Override
	public Object resolveArgument(MethodParameter parameter,
			ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
			WebDataBinderFactory binderFactory) throws Exception {
		    Subject subject = SecurityUtils.getSubject();
			ShiroDbRealm.ShiroUser shiroUser = (ShiroDbRealm.ShiroUser) subject.getPrincipal();
			LoginUser loginUser = new LoginUser();
			loginUser.setName(shiroUser.name);
			loginUser.setLoginName(shiroUser.loginName);
			loginUser.setId(shiroUser.id);
		    return loginUser;
	}

}